package com.gpay.erp.services.system.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import com.gpay.erp.core.dao.BaseDao;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.RoleTree;
import com.gpay.erp.core.system.bean.SysMenu;
import com.gpay.erp.core.system.bean.SysRole;
import com.gpay.erp.core.system.bean.SysUser;
import com.gpay.erp.core.system.bean.TreeNode;
import com.gpay.erp.services.system.RoleInteface;
import org.springframework.stereotype.Service;

/**
 * 用户业务逻辑实现类
 * 
 * @author comrose
 * 
 */
@Service
public class RoleService implements RoleInteface {

	@Resource
	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	public int insert(PagerModel e) {
		return dao.insert("role.addRole", e);
	}

	public int insertRoleUsers(SysUser e) {
		if (e.getRoleId() != null) {
			SysRole sysRole = new SysRole();
			sysRole.setId(e.getRoleId());
			dao.delete("role.deleteById3", sysRole);
			if (e.getUserIds() != null) {
				for (String userId : e.getUserIds()) {
					e.setUserId(userId);
					dao.insert("role.addRoleUser", e);
				}
			}
			return 1;
		}
		return 0;
	}

	public int inserts(RoleTree e) {
		int flag = 0;
		for (String nodeId : e.getChoiceId()) {
			e.setNodeId(nodeId);
			flag = dao.insert("role.addRoleNode", e);
		}
		if (flag != 0) {
			return 1;
		}
		return 0;
	}

	public int delete(PagerModel e) {
		return dao.delete("role.deleteById4", e);
	}

	public int deletes(String[] ids) {
		int flag;
		int s=0;
          if(ids!=null)
          {
		for (String id : ids) {
			SysRole sys=new SysRole();
			sys.setId(id);
			flag=(Integer) dao.selectOne("role.selectRoleUser", sys);
			s=s+flag;
		}
		if(s==0){
			for (String id : ids) {
				dao.delete("role.deleteById2", id);
				dao.delete("role.deleteById3", id);
				dao.delete("role.deleteById", id);
				}
			return 1;
			}else
			{
				return 2;
			}
		}          
          return 0;	
	}

	public int update(PagerModel e) {
		return dao.update("role.updateRole", e);
	}

	public PagerModel selectOne(PagerModel e) {
		return (PagerModel) dao.selectOne("role.selectRoleName", e);
	}

	public PagerModel selectById(String id) {
		return (PagerModel) dao.selectOne("role.selectOneNew", id);
	}

	public PagerModel selectPageList(PagerModel e) {
		return dao.selectPageList("role.selectPageList",
				"role.selectPageCount", e);
	}

	public List selectList(PagerModel e) {
		return null;
	}

	/*
	 * 角色权限下拉框
	 */
	public List findRoleMenu(SysRole e) {
		List menuList = dao.selectList("role.findeRoleMenu");
		if (e.getId() != null) {
			List roleMenuList = dao.selectList("role.queryATree", e);
			List treeNodeList = this.getTreeNode("-1", menuList, roleMenuList);
			return treeNodeList;
		}
		List treeNodeList = this.getTreeNode("-1", menuList);
		return treeNodeList;
	}

	// 功能树
	private List getTreeNode(String menuid, List listFunc, List listRoleFunc) {
		List resultList = new ArrayList();
		// 当前级菜单集合
		List list = this.getChildrens(listFunc, menuid);
		for (Object obj : list) {
			SysMenu tn = (SysMenu) obj;
			TreeNode treeNodeChecked = new TreeNode();
			treeNodeChecked.setText(tn.getName());
			treeNodeChecked.setId(tn.getNodeId());
			treeNodeChecked.setPreId(tn.getPreId());
			for (Object obj2 : listRoleFunc) {
				SysMenu tn2 = (SysMenu) obj2;
				if (tn2.getNodeId().equals(tn.getNodeId())) {
					if (checkLeafNode(tn2.getNodeId(), listFunc)) {
						treeNodeChecked.setChecked(true);
					}
				}
			}
			treeNodeChecked.setChildren(getTreeNode(tn.getNodeId(), listFunc,
					listRoleFunc));// 递归
			resultList.add(treeNodeChecked);
		}
		return resultList;
	}

	private List getTreeNode(String menuid, List listFunc) {
		List resultList = new ArrayList();
		// 当前级菜单集合
		List list = this.getChildrens(listFunc, menuid);
		for (Object obj : list) {
			SysMenu tn = (SysMenu) obj;
			TreeNode treeNodeChecked = new TreeNode();
			treeNodeChecked.setText(tn.getName());
			treeNodeChecked.setId(tn.getNodeId());
			treeNodeChecked.setPreId(tn.getPreId());
			treeNodeChecked.setChildren(getTreeNode(tn.getNodeId(), listFunc));// 递归
			resultList.add(treeNodeChecked);
		}
		return resultList;
	}

	// 子集合
	private List getChildrens(List funcs, String menuid) {
		List resultList = new ArrayList();
		SysMenu func = null;
		for (Object obj : funcs) {
			func = (SysMenu) obj;
			if (func.getPreId().equals(menuid)) {// 父节点id
				resultList.add(func);
			}
		}
		return resultList;
	}

	private Boolean checkLeafNode(String nodeIds, List funcs) {
		for (Object sobj : funcs) {
			SysMenu stn = (SysMenu) sobj;
			if (nodeIds.equals(stn.getPreId())) {
				return false;
			}
		}
		return true;

	}

	public List findUsers(SysRole e) {
		return dao.selectList("role.selectUserId", e);
	}

}
